// components/authorization.js
const App = getApp();
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    isAuthorization: {
      type: Boolean,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    flag: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    cancel() {
      this.triggerEvent('myevent', { flag: true });
    },
    onGotUserInfo(e) {
      const that = this;
      if (App.globalData.userInfo) {
        typeof cb == "function" && cb(App.globalData.userInfo)
      } else {
        //调用登录接口
        if (e.detail.errMsg === 'getUserInfo:ok') {
          App.globalData.userInfo = e.detail.userInfo;
          wx.login({
            success: function (res) {
              var args = {};
              args['code'] = res.code;
              args['grant_type'] = "authorization_code";
              wx.request({
                url: App.config() + 'index/getXiaoToken',
                data: args,
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  if (res.data.status == 0) {
                    var open_id = res.data.data.openid;
                    that.auto_login(open_id);
                    if (e.detail.userInfo) {
                      e.detail.userInfo['open_id'] = open_id;
                      wx.setStorage({
                        key: 'userInfo',
                        data: e.detail.userInfo
                      })
                      var params = {};
                      params['info'] = e.detail.userInfo;
                      that.submit_user_info(params)
                    }
                  }
                }
              })
            }
          })
        } else {
          that.triggerEvent('myevent', { flag: true });
        }
      }
    },
    submit_user_info (args) {
      const that = this;
      wx.request({
        url: App.config() + "index/xiaoWxInfo",
        data: args,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.cancel();
        }
      })
    },
    auto_login: function (open_id) {
      const that = this;
      var params = {};
      params['open_id'] = open_id;
      wx.request({
        url: App.config() + "member/xiaoLoginByWx",
        data: params,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          var data = res.data;
          if (data.status == 0) {
            // 登陆成功
            data.data['open_id'] = open_id;
            wx.setStorage({
              key: 'memberInfo',
              data: data.data,
              success: function() {
                that.triggerEvent('myevent', { flag: true });
              }
            })
          } else {
            //登陆失败
            var params = {};
            params['open_id'] = open_id;
            wx.setStorage({
              key: 'memberInfo',
              data: params,
            })
          }
        }
      })
    }
  },
})
